﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace Write_Read_fileEx
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = "D:\\k54.txt";

            //kiem tra file da ton tai chua
            //neu chua thi tao file moi va ghi du lieu vao do
            //neu da ton tai thi ghi noi tiep vao file do
            if (!File.Exists(filePath))
            {
                //tao file moi
                using (StreamWriter sw = File.CreateText(filePath))
                {
                    sw.WriteLine("Hello !");
                    sw.WriteLine("First Line");
                    sw.WriteLine("Second Line");
                }
            }
            else
            { 
                //ghi noi tiep vao file
                using (StreamWriter sw = File.AppendText(filePath))
                {
                    sw.WriteLine("Third Line");
                }
            }

            //
            //
            //

            //doc file
            if (File.Exists(filePath))
            { 
                //su dung StreamReader de doc file
                using (StreamReader sr = File.OpenText(filePath))
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                }
            }
            Console.ReadKey();
        }
    }
}
